package com.shujia.flink.core

import org.apache.flink.api.common.eventtime.{SerializableTimestampAssigner, WatermarkStrategy}
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow

import java.time.Duration

object Demo7EventTime {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    env.setParallelism(1)
    /**
     *  java,1667444174000
     *  java,1667444175000
     *  java,1667444176000
     *  java,1667444180000
     *  java,1667444179000
     *  java,1667444181000
     *  java,1667444182000
     *  java,1667444184000
     *  java,1667444185000
     */

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    //1、整理时间将时间字段取出来
    val tsDS: DataStream[(String, Long)] = linesDS.map(line => {
      val split: Array[String] = line.split(",")
      val word: String = split(0)
      //时间字段
      val ts: Long = split(1).toLong
      (word, ts)
    })

    /**
     * 2、需要告诉flink哪一个字段是时间字段
     * 默认值的水位线等于最新一条数据的时间戳，水位线只能增加不能降低
     */
    //val assDS: DataStream[(String, Long)] = tsDS.assignAscendingTimestamps(_._2)

    /**
     * 指定时间字段和水位线
     */
    val watermarkStrategy: WatermarkStrategy[(String, Long)] = WatermarkStrategy
      //将水位线前移5秒
      .forBoundedOutOfOrderness[(String, Long)](Duration.ofSeconds(5))
      //设置时间字段
      .withTimestampAssigner(new SerializableTimestampAssigner[(String, Long)] {
        override def extractTimestamp(element: (String, Long), recordTimestamp: Long): Long = element._2
      })
    val assDS: DataStream[(String, Long)] = tsDS.assignTimestampsAndWatermarks(watermarkStrategy)


    /**
     * 每隔5秒统计单词的数量
     */

    val kvDS: DataStream[(String, Int)] = assDS.map(kv => (kv._1, 1))


    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    /**
     * TumblingEventTimeWindows: 滚动的处理时间窗口
     *
     * 时间窗口
     * 1、窗口划分：将一分钟分钟整数等分成多个窗口
     * 2、窗口触发条件
     * 1、窗口i内有数据
     * 2、当大于等于窗口结束时间的数据来了会触发前一个窗口的计算
     */
    val windowDS: WindowedStream[(String, Int), String, TimeWindow] = keyByDS
      .window(TumblingEventTimeWindows.of(Time.seconds(5)))


    val countDS: DataStream[(String, Int)] = windowDS.sum(1)

    countDS.print()

    env.execute()

  }

}
